<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="代码重构,Android Studio重构,Android 重构," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="使用Android Studio对代码进行重构 简介2013年Google I/O大会上，谷歌推出新的Android开发环境——Android Studio，从此Android程序员有了新的选择，使用Android Studio进行App开发。AndroidStudio是一项全新的基于IntelliJ IDEA的Android开发环境。类似于Eclipse ADT插件，现在已经成为了官方推荐的id">
<meta name="keywords" content="代码重构,Android Studio重构,Android 重构">
<meta property="og:type" content="article">
<meta property="og:title" content="使用Android Studio对代码进行重构">
<meta property="og:url" content="http://mycommons.cn/2016/07/19/Android-Studio-Refactor/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="使用Android Studio对代码进行重构 简介2013年Google I/O大会上，谷歌推出新的Android开发环境——Android Studio，从此Android程序员有了新的选择，使用Android Studio进行App开发。AndroidStudio是一项全新的基于IntelliJ IDEA的Android开发环境。类似于Eclipse ADT插件，现在已经成为了官方推荐的id">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//1.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//2.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//3.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//4.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//5.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//6.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//7.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//8.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//9.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//10.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//11.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//12.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//13.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//14.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//15.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//16.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//17.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//18.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//19.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//20.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//21.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//22.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//23.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//24.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//100.gif">
<meta property="og:updated_time" content="2016-07-19T06:23:37.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用Android Studio对代码进行重构">
<meta name="twitter:description" content="使用Android Studio对代码进行重构 简介2013年Google I/O大会上，谷歌推出新的Android开发环境——Android Studio，从此Android程序员有了新的选择，使用Android Studio进行App开发。AndroidStudio是一项全新的基于IntelliJ IDEA的Android开发环境。类似于Eclipse ADT插件，现在已经成为了官方推荐的id">
<meta name="twitter:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2016/07/19/Android-Studio-Refactor/"/>





  <title>使用Android Studio对代码进行重构 | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2016/07/19/Android-Studio-Refactor/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">使用Android Studio对代码进行重构</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-07-19T14:22:24+08:00">
                2016-07-19
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/工具使用/" itemprop="url" rel="index">
                    <span itemprop="name">工具使用</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/07/19/Android-Studio-Refactor/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2016/07/19/Android-Studio-Refactor/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="使用Android-Studio对代码进行重构"><a href="#使用Android-Studio对代码进行重构" class="headerlink" title="使用Android Studio对代码进行重构"></a>使用Android Studio对代码进行重构</h1><hr>
<h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>2013年Google I/O大会上，谷歌推出新的Android开发环境——Android Studio，从此Android程序员有了新的选择，使用Android Studio进行App开发。AndroidStudio是一项全新的基于IntelliJ IDEA的Android开发环境。类似于Eclipse ADT插件，现在已经成为了官方推荐的ide，同时Eclipse不再进行更新。</p>
<p>软件开发中，经过几个版本迭代，不少程序员会觉的以前的代码架构可能不满足日益增长的需求，这时候都会想到了重构，关于重构网上也有不少用于的资料。</p>
<p><a href="http://blog.csdn.net/knightswarrior/article/details/9447815" target="_blank" rel="external">重构资料</a></p>
<p>资料里面列举了如下几种方法，对代码进行重构。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div></pre></td><td class="code"><pre><div class="line">31天重构学习笔记01. 封装集合</div><div class="line">31天重构学习笔记02. 移动方法</div><div class="line">31天重构学习笔记03. 提升方法</div><div class="line">31天重构学习笔记04. 降低方法</div><div class="line">31天重构学习笔记05. 提升字段</div><div class="line">31天重构学习笔记06. 降低字段</div><div class="line">31天重构学习笔记07. 重命名（方法，类，参数）</div><div class="line">31天重构学习笔记08. 使用委派代替继承</div><div class="line">31天重构学习笔记09. 提取接口</div><div class="line">31天重构学习笔记10. 提取方法</div><div class="line">31天重构学习笔记11. 使用策略类</div><div class="line">31天重构学习笔记12. 分解依赖</div><div class="line">31天重构学习笔记13. 提取方法对象</div><div class="line">31天重构学习笔记14. 分离职责</div><div class="line">31天重构学习笔记15. 移除重复内容</div><div class="line">31天重构学习笔记16. 封装条件</div><div class="line">31天重构学习笔记17. 提取父类</div><div class="line">31天重构学习笔记18. 使用条件判断代替异常</div><div class="line">31天重构学习笔记19. 提取工厂类</div><div class="line">31天重构学习笔记20. 提取子类</div><div class="line">31天重构学习笔记21. 合并继承</div><div class="line">31天重构学习笔记22. 分解方法</div><div class="line">31天重构学习笔记23. 引入参数对象</div><div class="line">31天重构学习笔记24. 分解复杂判断</div><div class="line">31天重构学习笔记25. 引入契约式设计</div><div class="line">31天重构学习笔记26. 避免双重否定</div><div class="line">31天重构学习笔记27. 去除上帝类</div><div class="line">31天重构学习笔记28. 为布尔方法命名</div><div class="line">31天重构学习笔记29. 去除中间人对象</div><div class="line">31天重构学习笔记30. 尽快返回</div><div class="line">31天重构学习笔记31. 使用多态代替条件判断</div></pre></td></tr></table></figure>
<p>Android Studio是基于优秀的ide的，ide提供了丰富的功能，很方便的对代码进行重构，下图是我的Android Studio Refactor菜单，部分快捷键与默认快捷键不同是因为方便而修改的。</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//1.png" alt="Android Studio Refactor"></p>
<p>古人云『工欲善其事必先利其器』，所以在对代码进行重构前，需要详细了解Android Studio提供的功能，下面对AS(Android Studio，后面简称AS)菜单进行简单示例。</p>
<h2 id="如何使用"><a href="#如何使用" class="headerlink" title="如何使用"></a>如何使用</h2><p>鼠标光标选中或者放在代码上，按下快捷键就可以弹出当前代码可以使用的功能。<br>如下所示：</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//1.gif" alt="如何使用"></p>
<h2 id="代码与操作示例"><a href="#代码与操作示例" class="headerlink" title="代码与操作示例"></a>代码与操作示例</h2><ul>
<li>ChangeSignature，改变函数签名，可以修改函数的名字，参数的顺序，参数的名字。</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">// 改变签名</div><div class="line">void testChangeSignature(int second, int first) &#123;</div><div class="line">    System.out.println(first + &quot;-&gt;&quot; + second);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//2.gif" alt="改变签名"></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">// 改变签名</div><div class="line">void testChangeSignature(int one, int two) &#123;</div><div class="line">    System.out.println(one + &quot;-&gt;&quot; + two);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>修改前参数依次是second、first，修改是one、two。</p>
<ul>
<li>ChangeClassSignatuere，改变类的签名，可以修改类的泛型签名</li>
</ul>
<p>Before：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">    // 改变类的签名</div><div class="line">    void testChangeClassSignatuere() &#123;</div><div class="line">        String second = &quot;second&quot;;</div><div class="line">        int first = 100;</div><div class="line">        new ChangeClassSignatuere(second, first);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">// **********************分割线*******************************    </div><div class="line">    </div><div class="line">public class ChangeClassSignatuere &#123;</div><div class="line"></div><div class="line">    private int first;</div><div class="line">    private String second;</div><div class="line"></div><div class="line">    public ChangeClassSignatuere(String second, int first) &#123;</div><div class="line">        this.first = first;</div><div class="line">        this.second = second;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public String toString() &#123;</div><div class="line">        return &quot;ChangeClassSignatuere&#123;&quot; +</div><div class="line">                &quot;second=&apos;&quot; + second + &apos;\&apos;&apos; +</div><div class="line">                &quot;,first=&quot; + first +</div><div class="line">                &apos;&#125;&apos;;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//3.gif" alt="改变类签名"></p>
<p>After：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div></pre></td><td class="code"><pre><div class="line">    // 改变类的签名</div><div class="line">    void testChangeClassSignatuere() &#123;</div><div class="line">        String second = &quot;second&quot;;</div><div class="line">        int first = 100;</div><div class="line">        new ChangeClassSignatuere&lt;Activity&gt;(second, first);</div><div class="line">    &#125;</div><div class="line">// **********************分割线*******************************    </div><div class="line"></div><div class="line">public class ChangeClassSignatuere&lt;A&gt; &#123;</div><div class="line"></div><div class="line">    private int first;</div><div class="line">    private String second;</div><div class="line"></div><div class="line">    public ChangeClassSignatuere(String second, int first) &#123;</div><div class="line">        this.first = first;</div><div class="line">        this.second = second;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public String toString() &#123;</div><div class="line">        return &quot;ChangeClassSignatuere&#123;&quot; +</div><div class="line">                &quot;second=&apos;&quot; + second + &apos;\&apos;&apos; +</div><div class="line">                &quot;,first=&quot; + first +</div><div class="line">                &apos;&#125;&apos;;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>修改匿名类为内部类</li>
</ul>
<p>Before：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">// 匿名类改成内部类</div><div class="line">void testConvertAnonymousToInner() &#123;</div><div class="line">    View.OnClickListener clickListener = new View.OnClickListener() &#123;</div><div class="line"></div><div class="line">        @Override</div><div class="line">        public void onClick(View v) &#123;</div><div class="line">            System.out.println(&quot;onClick&quot;);</div><div class="line">        &#125;</div><div class="line">    &#125;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//4.gif" alt="改变类签名"></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">// 匿名类改成内部类</div><div class="line">void testConvertAnonymousToInner() &#123;</div><div class="line"></div><div class="line">    View.OnClickListener clickListener = new Abc123();</div><div class="line">&#125;</div><div class="line"></div><div class="line">private static class Abc123 implements View.OnClickListener &#123;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void onClick(View v) &#123;</div><div class="line">        System.out.println(&quot;onClick&quot;);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这个是我最喜欢的一个功能，有时候匿名类刚刚开始逻辑很简单，过不了多久逻辑太多，就可以使用快捷键变成一个内部类。</p>
<ul>
<li>ConvertToInstanceMethod，修改一个方法变成成员方法</li>
</ul>
<p>Befor:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">    // 变成成员方法</div><div class="line">    void testConvertToInstanceMethod() &#123;</div><div class="line">        TestClass.convertToInstanceMethod(this, &quot;test&quot;);</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">public class TestClass &#123;</div><div class="line"></div><div class="line">    static void convertToInstanceMethod(RefactorDemo demo, String string) &#123;</div><div class="line">        System.out.println(&quot;convertToInstanceMethod = &quot; + demo);</div><div class="line">        System.out.println(&quot;convertToInstanceMethod = &quot; + string);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 变成成员方法</div><div class="line">void testConvertToInstanceMethod() &#123;</div><div class="line">    this.convertToInstanceMethod(&quot;test&quot;);</div><div class="line">&#125;</div><div class="line"></div><div class="line">void convertToInstanceMethod(String string) &#123;</div><div class="line">    System.out.println(&quot;convertToInstanceMethod = &quot; + this);</div><div class="line">    System.out.println(&quot;convertToInstanceMethod = &quot; + string);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在修改前，调用convertToInstanceMethod方法，第一个参数相是RefactorDemo，传递是this， 修改后就那个方法直接移动到本类中。</p>
<ul>
<li>Copy，复制一个类</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">    // 复制一个类</div><div class="line">    void testCopy() &#123;</div><div class="line">        new FirstClass();</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">public class FirstClass implements Serializable &#123;</div><div class="line"></div><div class="line">    public String first;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public String toString() &#123;</div><div class="line">        return &quot;FirstClass&#123;&quot; +</div><div class="line">                &quot;first=&apos;&quot; + first + &apos;\&apos;&apos; +</div><div class="line">                &apos;&#125;&apos;;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//5.gif" alt="复制一个类"></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">public class SecondClass implements Serializable &#123;</div><div class="line"></div><div class="line">    public String first;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public String toString() &#123;</div><div class="line">        return &quot;SecondClass&#123;&quot; +</div><div class="line">                &quot;first=&apos;&quot; + first + &apos;\&apos;&apos; +</div><div class="line">                &apos;&#125;&apos;;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>EncapsulateFields，压缩一个字段，间接访问</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">String filed = &quot;filed&quot;;</div><div class="line"></div><div class="line">// 压缩一个字段，间接访问</div><div class="line">void testEncapsulateFields() &#123;</div><div class="line">    System.out.println(filed);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">private String filed = &quot;filed&quot;;</div><div class="line"></div><div class="line">// 压缩一个字段，间接访问</div><div class="line">void testEncapsulateFields() &#123;</div><div class="line">    System.out.println(getFiled());</div><div class="line">&#125;</div><div class="line"></div><div class="line">public String getFiled() &#123;</div><div class="line">    return filed;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void setFiled(String filed) &#123;</div><div class="line">    this.filed = filed;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>相当于简介访问一个field，自动生成setter和getter。</p>
<ul>
<li>GenerifyRefactoring，泛型重构</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">// 泛型重构</div><div class="line">void testGenerifyRefactoring() &#123;</div><div class="line">    List list = new ArrayList();</div><div class="line">    list.add(&quot;one&quot;);</div><div class="line">    list.add(&quot;two&quot;);</div><div class="line">    list.add(&quot;three&quot;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">// 泛型重构</div><div class="line">void testGenerifyRefactoring() &#123;</div><div class="line">    List&lt;String&gt; list = new ArrayList&lt;String&gt;();</div><div class="line">    list.add(&quot;one&quot;);</div><div class="line">    list.add(&quot;two&quot;);</div><div class="line">    list.add(&quot;three&quot;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>自动添加泛型的参数。</p>
<ul>
<li>Inline，内联函数</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">// 内联</div><div class="line">void testInline() &#123;</div><div class="line">    int a = 100;</div><div class="line">    int b = 200;</div><div class="line">    System.out.println(add(a, b));</div><div class="line">&#125;</div><div class="line"></div><div class="line">int add(int a, int b) &#123;</div><div class="line">    return a + b;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//6.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">// 内联</div><div class="line">void testInline() &#123;</div><div class="line">    int a = 100;</div><div class="line">    int b = 200;</div><div class="line">    System.out.println(a + b);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>原先需要调用一个函数的地方，直接把那个函数里面的代码复制过来。</p>
<ul>
<li>InvertBoolean，重构Boolean</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">// 重构Boolean</div><div class="line">void testInvertBoolean() &#123;</div><div class="line">    System.out.println(checkPaswd(null));</div><div class="line">    System.out.println(checkPaswd(&quot;&quot;));</div><div class="line">    System.out.println(checkPaswd(&quot;admin&quot;));</div><div class="line">&#125;</div><div class="line"></div><div class="line">boolean checkPaswd(String passwd) &#123;</div><div class="line">    if (passwd != null &amp;&amp; passwd.length() != 0) &#123;</div><div class="line">        return true;</div><div class="line">    &#125;</div><div class="line">    return false;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//7.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">// 重构Boolean</div><div class="line">void testInvertBoolean() &#123;</div><div class="line">    System.out.println(checkPaswd(null));</div><div class="line">    System.out.println(checkPaswd(&quot;&quot;));</div><div class="line">    System.out.println(checkPaswd(&quot;admin&quot;));</div><div class="line">&#125;</div><div class="line"></div><div class="line">boolean checkPaswd(String passwd) &#123;</div><div class="line">    return passwd != null &amp;&amp; passwd.length() != 0;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>MakeClassStatic，使类变成静态的</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">// 使类变成静态的</div><div class="line">void testMakeClassStatic() &#123;</div><div class="line">    new MyClass().fun();</div><div class="line">&#125;</div><div class="line"></div><div class="line">String myClassField = &quot;abc&quot;;</div><div class="line"></div><div class="line">class MyClass &#123;</div><div class="line">    void fun() &#123;</div><div class="line">        System.out.println(&quot;myClassField = &quot; + myClassField);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//8.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">// 使类变成静态的</div><div class="line">void testMakeClassStatic() &#123;</div><div class="line">    new MyClass(myClassField).fun();</div><div class="line">&#125;</div><div class="line"></div><div class="line">String myClassField = &quot;abc&quot;;</div><div class="line"></div><div class="line">static class MyClass &#123;</div><div class="line">    private String myClassField;</div><div class="line"></div><div class="line">    public MyClass(String myClassField) &#123;</div><div class="line">        this.myClassField = myClassField;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    void fun() &#123;</div><div class="line">        System.out.println(&quot;myClassField = &quot; + myClassField);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>修改前需要调用外部内的成员变量myClassField，实际编译过后MyClass的class是持有外部类的对象，这样才能访问myClassField，通过重构，使得myClassField通过构造方法传入进去，然后再在fun方法中使用，这样重构会减少类的依赖。</p>
<ul>
<li>MakeMethodStatic，使方法变成静态的</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">// 使方法变成静态的</div><div class="line">void testMakeMethodStatic() &#123;</div><div class="line">    myFun();</div><div class="line">&#125;</div><div class="line"></div><div class="line">void myFun() &#123;</div><div class="line">    System.out.println(&quot;myFun&quot;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">// 使方法变成静态的</div><div class="line">void testMakeMethodStatic() &#123;</div><div class="line">    myFun();</div><div class="line">&#125;</div><div class="line"></div><div class="line">static void myFun() &#123;</div><div class="line">    System.out.println(&quot;myFun&quot;);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>Move，移动一个类</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">// 移动一个类</div><div class="line">void testMove() &#123;</div><div class="line">    new MoveClass().fun();</div><div class="line">&#125;</div><div class="line"></div><div class="line">static class MoveClass &#123;</div><div class="line"></div><div class="line">    String movde;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public String toString() &#123;</div><div class="line">        return &quot;MoveClass&#123;&quot; +</div><div class="line">                &quot;movde=&apos;&quot; + movde + &apos;\&apos;&apos; +</div><div class="line">                &apos;&#125;&apos;;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    void fun() &#123;</div><div class="line">        System.out.println(this);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//9.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">    // 移动一个类</div><div class="line">    void testMove() &#123;</div><div class="line">        new MoveClassxxxxxx().fun();</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">class MoveClassxxxxxx &#123;</div><div class="line"></div><div class="line">    String movde;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public String toString() &#123;</div><div class="line">        return &quot;MoveClass&#123;&quot; +</div><div class="line">                &quot;movde=&apos;&quot; + movde + &apos;\&apos;&apos; +</div><div class="line">                &apos;&#125;&apos;;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    void fun() &#123;</div><div class="line">        System.out.println(this);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>PullMenmberUp，上移</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">public class Base &#123;</div><div class="line"></div><div class="line">    public String baseString;</div><div class="line"></div><div class="line">    public Base(String baseString) &#123;</div><div class="line">        this.baseString = baseString;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funBase() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">public class Sub extends Base &#123;</div><div class="line"></div><div class="line"></div><div class="line">    public Sub(String baseString) &#123;</div><div class="line">        super(baseString);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funSub() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">    // 上移</div><div class="line">    void testPullMenmberUp() &#123;</div><div class="line">        new Sub(&quot;base&quot;).funSub();</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//10.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">public class Base &#123;</div><div class="line"></div><div class="line">    public String baseString;</div><div class="line"></div><div class="line">    public Base(String baseString) &#123;</div><div class="line">        this.baseString = baseString;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funBase() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funSub() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class Sub extends Base &#123;</div><div class="line"></div><div class="line"></div><div class="line">    public Sub(String baseString) &#123;</div><div class="line">        super(baseString);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>实际上就是把子类中的方法移动到父类中。</p>
<ul>
<li>PullMenmberDown，下移</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line">public class Base &#123;</div><div class="line"></div><div class="line">    public String baseString;</div><div class="line"></div><div class="line">    public Base(String baseString) &#123;</div><div class="line">        this.baseString = baseString;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funBase() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">public class Sub extends Base &#123;</div><div class="line"></div><div class="line"></div><div class="line">    public Sub(String baseString) &#123;</div><div class="line">        super(baseString);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funSub() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">    // 下移</div><div class="line">    void testPullMenmberDown() &#123;</div><div class="line">        new Sub(&quot;base&quot;).funBase();</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line">public class Base &#123;</div><div class="line"></div><div class="line">    public String baseString;</div><div class="line"></div><div class="line">    public Base(String baseString) &#123;</div><div class="line">        this.baseString = baseString;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line">public class Sub extends Base &#123;</div><div class="line"></div><div class="line">    public Sub(String baseString) &#123;</div><div class="line">        super(baseString);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funSub() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void funBase() &#123;</div><div class="line">        System.out.println(baseString);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>实际上就是把父类中的方法移动到子类中。</p>
<ul>
<li>Rename，重命名</li>
</ul>
<p>这个功能也是我最喜欢的，只要选择重命名一个对象或者资源，所有使用到这个对象或者资源的地方，都会进行重新命名。</p>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">// 重命名</div><div class="line">void testRename() &#123;</div><div class="line">    String first = &quot;second&quot;;</div><div class="line">    System.out.println(first);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//11.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">// 重命名</div><div class="line">void testRename() &#123;</div><div class="line">    String second = &quot;second&quot;;</div><div class="line">    System.out.println(second);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ReplaceConstructorWithBuilder，构造方法变成builder</li>
</ul>
<p>还在羡慕Picasso，Fresco人性化调用方式吗？很简单，通过这个功能就可以快速生成代码</p>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">public class MyAlertDialog &#123;</div><div class="line"></div><div class="line">    private String title;</div><div class="line">    private String message;</div><div class="line">    private String okButton;</div><div class="line">    private String cancelButton;</div><div class="line"></div><div class="line">    public MyAlertDialog(String title, String message, String okButton, String cancelButton) &#123;</div><div class="line">        this.title = title;</div><div class="line">        this.message = message;</div><div class="line">        this.okButton = okButton;</div><div class="line">        this.cancelButton = cancelButton;</div><div class="line">    &#125;</div><div class="line">&#125;    </div><div class="line">    // 构造方法变成builder</div><div class="line">    void testReplaceConstructorWithBuilder() &#123;</div><div class="line">        new MyAlertDialog(&quot;title&quot;, &quot;message&quot;, &quot;ok&quot;, &quot;cancel&quot;).show();</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//12.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div></pre></td><td class="code"><pre><div class="line">    // 构造方法变成builder</div><div class="line">    void testReplaceConstructorWithBuilder() &#123;</div><div class="line">        new MyAlertDialog.Builder()</div><div class="line">                .setTitle(&quot;title&quot;)</div><div class="line">                .setMessage(&quot;message&quot;)</div><div class="line">                .setOkButton(&quot;ok&quot;)</div><div class="line">                .setCancelButton(&quot;cancel&quot;)</div><div class="line">                .createMyAlertDialog()</div><div class="line">                .show();</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">public static class Builder &#123;</div><div class="line">    private String title;</div><div class="line">    private String message;</div><div class="line">    private String okButton;</div><div class="line">    private String cancelButton;</div><div class="line"></div><div class="line">    public Builder setTitle(String title) &#123;</div><div class="line">        this.title = title;</div><div class="line">        return this;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public Builder setMessage(String message) &#123;</div><div class="line">        this.message = message;</div><div class="line">        return this;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public Builder setOkButton(String okButton) &#123;</div><div class="line">        this.okButton = okButton;</div><div class="line">        return this;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public Builder setCancelButton(String cancelButton) &#123;</div><div class="line">        this.cancelButton = cancelButton;</div><div class="line">        return this;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public MyAlertDialog createMyAlertDialog() &#123;</div><div class="line">        return new MyAlertDialog(title, message, okButton, cancelButton);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ReplaceConstructorWithFactory，构造方法变成工程方法</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">// 构造方法变成工程方法</div><div class="line">void testReplaceConstructorWithFactory() &#123;</div><div class="line">    new MyAlertDialog(&quot;title&quot;, &quot;message&quot;, &quot;ok&quot;, &quot;cancel&quot;).show();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//13.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">    // 构造方法变成工程方法</div><div class="line">    void testReplaceConstructorWithFactory() &#123;</div><div class="line">        MyAlertDialog.newInstance(&quot;title&quot;, &quot;message&quot;, &quot;ok&quot;, &quot;cancel&quot;)</div><div class="line">                .show();</div><div class="line">    &#125;</div><div class="line">public class MyAlertDialog &#123;    </div><div class="line">    private MyAlertDialog(String title, String message, String okButton, String cancelButton) &#123;</div><div class="line">        this.title = title;</div><div class="line">        this.message = message;</div><div class="line">        this.okButton = okButton;</div><div class="line">        this.cancelButton = cancelButton;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public static MyAlertDialog newInstance(String title, String message, String okButton, String cancelButton) &#123;</div><div class="line">        return new MyAlertDialog(title, message, okButton, cancelButton);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>通过上面代码发现，如果构造方法变成了工厂方式，那么它的构造参数是private的，这样调用者只能通过工厂方式来生成对象。</p>
<ul>
<li>ReplaceInheritanceWithDelegation，代理代替继承</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">public abstract class AbsClass &#123;</div><div class="line"></div><div class="line">    public abstract void sayHello();</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class ExtClass extends AbsClass &#123;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void sayHello() &#123;</div><div class="line">        System.out.println(&quot;hello&quot;);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">    // 代理代替继承</div><div class="line">    void testReplaceInheritanceWithDelegation() &#123;</div><div class="line">        new ExtClass().sayHello();</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//14.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">public class ExtClass &#123;</div><div class="line"></div><div class="line">    private final AbsClassImpl abs = new AbsClassImpl();</div><div class="line"></div><div class="line">    public void sayHello() &#123;</div><div class="line">        abs.sayHello();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    private class AbsClassImpl extends AbsClass &#123;</div><div class="line">        @Override</div><div class="line">        public void sayHello() &#123;</div><div class="line">            System.out.println(&quot;hello&quot;);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">    // 代理代替继承</div><div class="line">    void testReplaceInheritanceWithDelegation() &#123;</div><div class="line">        new ExtClass().sayHello();</div><div class="line">    &#125;</div></pre></td></tr></table></figure>
<p>书上说过，组合由于继承大概说的就是这个意思。</p>
<ul>
<li>SafeDelete，安全删除</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">// 安全删除</div><div class="line">void testSafeDelete() &#123;</div><div class="line">    String unUsedString = &quot;abc&quot;;</div><div class="line"></div><div class="line">    unUsedString = getUnUsedString();</div><div class="line">    System.out.println(new Date());</div><div class="line">&#125;</div><div class="line"></div><div class="line">public String getUnUsedString() &#123;</div><div class="line">    return unUsedString;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">// 安全删除</div><div class="line">void testSafeDelete() &#123;</div><div class="line">    System.out.println(new Date());</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>这个比较简单，如果某个变量没有使用，那么直接直接删除所有引用这个变量的地方。</p>
<ul>
<li>WrapReturnValue，封装返回值</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">// 封装返回值</div><div class="line">void testWrapReturnValue() &#123;</div><div class="line">    System.out.println(getUserName());</div><div class="line">&#125;</div><div class="line"></div><div class="line">private String getUserName() &#123;</div><div class="line">    return &quot;userName&quot;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//15.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">// 包装下返回值</div><div class="line">void testWrapReturnValue() &#123;</div><div class="line">    System.out.println(getUserName().getValue());</div><div class="line">&#125;</div><div class="line"></div><div class="line">private UserInfo getUserName() &#123;</div><div class="line">    return new UserInfo(&quot;userName&quot;);</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class UserInfo &#123;</div><div class="line">    private final String value;</div><div class="line"></div><div class="line">    public UserInfo(String value) &#123;</div><div class="line">        this.value = value;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public String getValue() &#123;</div><div class="line">        return value;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>RemoveMiddleMan，去除中间人</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">// 去除中间人</div><div class="line">void testRemoveMiddleMan() &#123;</div><div class="line">    OrderManager manager = new OrderManager();</div><div class="line">    manager.newOrder(&quot;new&quot;);</div><div class="line">    manager.confirm(&quot;confir&quot;);</div><div class="line">    manager.invalidOrder(&quot;invalid&quot;);</div><div class="line">&#125;</div><div class="line"></div><div class="line">public static class OrderManager &#123;</div><div class="line">    private List&lt;String&gt; unProcessOrderList;</div><div class="line">    private List&lt;String&gt; processedOrderList;</div><div class="line"></div><div class="line">    public OrderManager() &#123;</div><div class="line">        unProcessOrderList = new ArrayList&lt;&gt;();</div><div class="line">        processedOrderList = new ArrayList&lt;&gt;();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void newOrder(String order) &#123;</div><div class="line">        unProcessOrderList.add(order);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void confirm(String order) &#123;</div><div class="line">        unProcessOrderList.remove(order);</div><div class="line">        processedOrderList.add(order);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void invalidOrder(String order) &#123;</div><div class="line">        processedOrderList.remove(order);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void display() &#123;</div><div class="line">        System.out.println(unProcessOrderList);</div><div class="line">        System.out.println(processedOrderList);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//16.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">// 去除中间人</div><div class="line">void testRemoveMiddleMan() &#123;</div><div class="line">    OrderManager manager = new OrderManager();</div><div class="line">    manager.getUnProcessOrderList().add(&quot;new&quot;);</div><div class="line">    manager.confirm(&quot;confir&quot;);</div><div class="line">    manager.getProcessedOrderList().remove(&quot;invalid&quot;);</div><div class="line">&#125;</div><div class="line"></div><div class="line">public static class OrderManager &#123;</div><div class="line">    private List&lt;String&gt; unProcessOrderList;</div><div class="line">    private List&lt;String&gt; processedOrderList;</div><div class="line"></div><div class="line">    public OrderManager() &#123;</div><div class="line">        unProcessOrderList = new ArrayList&lt;&gt;();</div><div class="line">        processedOrderList = new ArrayList&lt;&gt;();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void confirm(String order) &#123;</div><div class="line">        unProcessOrderList.remove(order);</div><div class="line">        processedOrderList.add(order);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public void display() &#123;</div><div class="line">        System.out.println(unProcessOrderList);</div><div class="line">        System.out.println(processedOrderList);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public List&lt;String&gt; getUnProcessOrderList() &#123;</div><div class="line">        return unProcessOrderList;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public List&lt;String&gt; getProcessedOrderList() &#123;</div><div class="line">        return processedOrderList;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>用途就是，原先OrderManager持有一个List来保存Order，调用方直接使用OrderManager来处理订单，去除中间人的意思就是说，调用法直接获取保存Order的容器，调用方直接自己控制容器。</p>
<h2 id="代码抽取"><a href="#代码抽取" class="headerlink" title="代码抽取"></a>代码抽取</h2><p>代码抽取在实际使用当中非常有用。</p>
<ul>
<li>ExtraMethod，抽取一个方法</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取一个方法</div><div class="line">void testExtraMethod() &#123;</div><div class="line">    Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</div><div class="line">    map.put(&quot;one&quot;, 1);</div><div class="line">    map.put(&quot;two&quot;, 2);</div><div class="line">    map.put(&quot;three&quot;, 2);</div><div class="line"></div><div class="line">    System.out.println(map);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//17.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">// 抽取一个方法</div><div class="line">void testExtraMethod() &#123;</div><div class="line">    Map&lt;String, Integer&gt; map = initMap();</div><div class="line"></div><div class="line">    System.out.println(map);</div><div class="line">&#125;</div><div class="line"></div><div class="line">@NonNull</div><div class="line">private Map&lt;String, Integer&gt; initMap() &#123;</div><div class="line">    Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</div><div class="line">    map.put(&quot;one&quot;, 1);</div><div class="line">    map.put(&quot;two&quot;, 2);</div><div class="line">    map.put(&quot;three&quot;, 2);</div><div class="line">    return map;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ExtraMethodObject，抽取一个方法到一个对象中</li>
</ul>
<p>和上面那个差不多，只不过把方法移动到另外一个类中。</p>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取一个方法到一个对象中</div><div class="line">void testExtraMethodObject() &#123;</div><div class="line">    Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</div><div class="line">    map.put(&quot;one&quot;, 1);</div><div class="line">    map.put(&quot;two&quot;, 22);</div><div class="line">    map.put(&quot;three&quot;, 33);</div><div class="line"></div><div class="line">    System.out.println(map);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">// 抽取一个方法到一个对象中</div><div class="line">void testExtraMethodObject() &#123;</div><div class="line">    Map&lt;String, Integer&gt; map = MapUtil.invoke();</div><div class="line">    System.out.println(map);</div><div class="line">&#125;</div><div class="line"></div><div class="line">private static class MapUtil &#123;</div><div class="line">    private static Map&lt;String, Integer&gt; invoke() &#123;</div><div class="line">        Map&lt;String, Integer&gt; map = new HashMap&lt;&gt;();</div><div class="line">        map.put(&quot;one&quot;, 1);</div><div class="line">        map.put(&quot;two&quot;, 22);</div><div class="line">        map.put(&quot;three&quot;, 33);</div><div class="line">        return map;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ExtractParameterObject，抽取若干参数成一个类</li>
</ul>
<p>这个比较使用，有时候一个方法参数太多，可以把这些参数合并成一个类。</p>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取若干参数成一个类</div><div class="line">void testExtractParameterObject() &#123;</div><div class="line">    print(100, 200);</div><div class="line">&#125;</div><div class="line"></div><div class="line">// widht,height --&gt; Size Class</div><div class="line">void print(int width, int height) &#123;</div><div class="line">    System.out.println(&quot;width = &quot; + width + &quot;, height = &quot; + height);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//18.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">    // 抽取若干参数成一个类</div><div class="line">    void testExtractParameterObject() &#123;</div><div class="line">        print(new Size(100, 200));</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    // widht,height --&gt; Size Class</div><div class="line">    void print(Size size) &#123;</div><div class="line">        System.out.println(&quot;width = &quot; + size.getWidth() + &quot;, height = &quot; + size.getHeight());</div><div class="line">    &#125;</div><div class="line">    </div><div class="line">    </div><div class="line">public class Size &#123;</div><div class="line">    private final int width;</div><div class="line">    private final int height;</div><div class="line"></div><div class="line">    public Size(int width, int height) &#123;</div><div class="line">        this.width = width;</div><div class="line">        this.height = height;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public int getWidth() &#123;</div><div class="line">        return width;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    public int getHeight() &#123;</div><div class="line">        return height;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ExtractSuperclass，抽取到父类</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">// 抽取到父类</div><div class="line">void testExtractSuperclass() &#123;</div><div class="line">    sendEvent(&quot;login_success&quot;);</div><div class="line">&#125;</div><div class="line"></div><div class="line">public void sendEvent(Object event) &#123;</div><div class="line">    // EventBus.getDefault().send(event);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//19.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">public class BaseExtractDemo &#123;</div><div class="line">    public void sendEvent(Object event) &#123;</div><div class="line">        // EventBus.getDefault().send(event);</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">public class ExtractDemo extends BaseExtractDemo &#123;</div><div class="line">    // 抽取到父类</div><div class="line">    void testExtractSuperclass() &#123;</div><div class="line">        sendEvent(&quot;login_success&quot;);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ExtractConstant，抽取常量</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取常量</div><div class="line">void testExtractConstant() &#123;</div><div class="line">    String email = &quot;admin@123.com&quot;;</div><div class="line">    String passwd = &quot;1qaz2wsx&quot;;</div><div class="line"></div><div class="line">    Bundle bundle = new Bundle();</div><div class="line">    bundle.putString(&quot;email&quot;, email);</div><div class="line">    bundle.putString(&quot;passwd&quot;, passwd);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//20.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">public static final String KEY_EMAIL = &quot;email&quot;;</div><div class="line">public static final String KEY_PASSWD = &quot;passwd&quot;;</div><div class="line"></div><div class="line">// 抽取常量</div><div class="line">void testExtractConstant() &#123;</div><div class="line">    String email = &quot;admin@123.com&quot;;</div><div class="line">    String passwd = &quot;1qaz2wsx&quot;;</div><div class="line"></div><div class="line">    Bundle bundle = new Bundle();</div><div class="line">    bundle.putString(KEY_EMAIL, email);</div><div class="line">    bundle.putString(KEY_PASSWD, passwd);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ExtractField，抽取成成员变量</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">// 抽取成成员变量</div><div class="line">void testExtractField() &#123;</div><div class="line">    String testExtractField =  &quot;testExtractField&quot;;</div><div class="line">    System.out.println(testExtractField);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">private String testExtractField;</div><div class="line"></div><div class="line">// 抽取成成员变量</div><div class="line">void testExtractField() &#123;</div><div class="line">    testExtractField = &quot;testExtractField&quot;;</div><div class="line">    System.out.println(testExtractField);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<ul>
<li>ExtractVariable，抽取成变量</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">// 抽取成变量</div><div class="line">void testExtractVariable() &#123;</div><div class="line">    System.out.println(&quot;long name = &quot; + getLoooooooooooooooooooooooooooooooooooooooooooooooooooooongName());</div><div class="line">&#125;</div><div class="line"></div><div class="line">String getLoooooooooooooooooooooooooooooooooooooooooooooooooooooongName() &#123;</div><div class="line">    return &quot;getLoooooooooooooooooooooooooooooooooooooooooooooooooooooongName&quot;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//21.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取成变量</div><div class="line">void testExtractVariable() &#123;</div><div class="line">    String name = getLoooooooooooooooooooooooooooooooooooooooooooooooooooooongName();</div><div class="line">    System.out.println(&quot;long name = &quot; + name);</div><div class="line">&#125;</div><div class="line"></div><div class="line">String getLoooooooooooooooooooooooooooooooooooooooooooooooooooooongName() &#123;</div><div class="line">    return &quot;getLoooooooooooooooooooooooooooooooooooooooooooooooooooooongName&quot;;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>有时候，一行代码写的很长，可以使用这个方法，对代码进行重构。</p>
<ul>
<li>ExtractParameter，抽取成方法的参数</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取成方法的参数</div><div class="line">void testExtractParameter() &#123;</div><div class="line">    printHelloString();</div><div class="line">&#125;</div><div class="line"></div><div class="line">private void printHelloString() &#123;</div><div class="line">    String str = &quot;printHello&quot;;</div><div class="line">    System.out.println(str);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//22.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">// 抽取成方法的参数</div><div class="line">void testExtractParameter() &#123;</div><div class="line">    String str = &quot;printHello&quot;;</div><div class="line">    printStringAndLength(str);</div><div class="line">&#125;</div><div class="line"></div><div class="line">private void printStringAndLength(String paramStr) &#123;</div><div class="line">    System.out.println(paramStr + &quot; -&gt; &quot; + paramStr.length());</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="Android资源重构"><a href="#Android资源重构" class="headerlink" title="Android资源重构"></a>Android资源重构</h2><ul>
<li>ExtractLayout，布局文件抽取</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</div><div class="line">&lt;android.support.design.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</div><div class="line">    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;</div><div class="line">    android:layout_width=&quot;match_parent&quot;</div><div class="line">    android:layout_height=&quot;match_parent&quot;</div><div class="line">    android:fitsSystemWindows=&quot;true&quot;&gt;</div><div class="line"></div><div class="line">    &lt;android.support.design.widget.AppBarLayout</div><div class="line">        android:layout_width=&quot;match_parent&quot;</div><div class="line">        android:layout_height=&quot;wrap_content&quot;</div><div class="line">        android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;&gt;</div><div class="line"></div><div class="line">        &lt;android.support.v7.widget.Toolbar</div><div class="line">            android:id=&quot;@+id/toolbar&quot;</div><div class="line">            android:layout_width=&quot;match_parent&quot;</div><div class="line">            android:layout_height=&quot;?attr/actionBarSize&quot;</div><div class="line">            android:background=&quot;?attr/colorPrimary&quot;</div><div class="line">            app:popupTheme=&quot;@style/AppTheme.PopupOverlay&quot; /&gt;</div><div class="line"></div><div class="line">    &lt;/android.support.design.widget.AppBarLayout&gt;</div><div class="line"></div><div class="line">    &lt;include layout=&quot;@layout/content_main&quot; /&gt;</div><div class="line"></div><div class="line">    &lt;android.support.design.widget.FloatingActionButton</div><div class="line">        android:id=&quot;@+id/fab&quot;</div><div class="line">        android:layout_width=&quot;wrap_content&quot;</div><div class="line">        android:layout_height=&quot;wrap_content&quot;</div><div class="line">        android:layout_gravity=&quot;bottom|end&quot;</div><div class="line">        android:layout_margin=&quot;@dimen/fab_margin&quot;</div><div class="line">        android:src=&quot;@android:drawable/ic_dialog_email&quot; /&gt;</div><div class="line"></div><div class="line">&lt;/android.support.design.widget.CoordinatorLayout&gt;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//23.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;</div><div class="line">&lt;android.support.design.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;</div><div class="line">    android:layout_width=&quot;match_parent&quot;</div><div class="line">    android:layout_height=&quot;match_parent&quot;</div><div class="line">    android:fitsSystemWindows=&quot;true&quot;&gt;</div><div class="line"></div><div class="line">    &lt;include layout=&quot;@layout/view_top&quot; /&gt;</div><div class="line"></div><div class="line">    &lt;include layout=&quot;@layout/content_main&quot; /&gt;</div><div class="line"></div><div class="line">    &lt;include layout=&quot;@layout/view_button&quot; /&gt;</div><div class="line"></div><div class="line">&lt;/android.support.design.widget.CoordinatorLayout&gt;</div></pre></td></tr></table></figure>
<ul>
<li>ExtractStyle，样式抽取</li>
</ul>
<p>Before:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">&lt;TextView</div><div class="line">    android:layout_width=&quot;wrap_content&quot;</div><div class="line">    android:layout_height=&quot;wrap_content&quot;</div><div class="line">    android:text=&quot;Hello World!&quot;</div><div class="line">    android:textAlignment=&quot;center&quot;</div><div class="line">    android:textColor=&quot;#f00ff0&quot;</div><div class="line">    android:textSize=&quot;18sp&quot;</div><div class="line">    android:textStyle=&quot;bold&quot;</div><div class="line">    android:typeface=&quot;normal&quot; /&gt;</div></pre></td></tr></table></figure>
<p>Gif:</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//24.gif" alt=""></p>
<p>After:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">&lt;TextView</div><div class="line">    android:layout_width=&quot;wrap_content&quot;</div><div class="line">    android:layout_height=&quot;wrap_content&quot;</div><div class="line">    android:text=&quot;Hello World!&quot;</div><div class="line">    style=&quot;@style/my_textview_style&quot; /&gt;</div><div class="line">    </div><div class="line">&lt;style name=&quot;my_textview_style&quot;&gt;</div><div class="line">    &lt;item name=&quot;android:textAlignment&quot;&gt;center&lt;/item&gt;</div><div class="line">    &lt;item name=&quot;android:textColor&quot;&gt;#f00ff0&lt;/item&gt;</div><div class="line">    &lt;item name=&quot;android:textSize&quot;&gt;18sp&lt;/item&gt;</div><div class="line">    &lt;item name=&quot;android:textStyle&quot;&gt;bold&lt;/item&gt;</div><div class="line">    &lt;item name=&quot;android:typeface&quot;&gt;normal&lt;/item&gt;</div><div class="line">&lt;/style&gt;</div></pre></td></tr></table></figure>
<h1 id="综合示例"><a href="#综合示例" class="headerlink" title="综合示例"></a>综合示例</h1><p>通过一个示例演示怎么样重构代码，示例是一个Activity里面有个RecyclerView，然后重构代码，演示怎么样分离Adapter,ViewHolder等。</p>
<p>Before:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line">public class StartActivity extends AppCompatActivity &#123;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_start);</div><div class="line">        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);</div><div class="line">        setSupportActionBar(toolbar);</div><div class="line"></div><div class="line">        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);</div><div class="line">        recyclerView.setLayoutManager(new LinearLayoutManager(this));</div><div class="line"></div><div class="line">        final List&lt;String&gt; data = new ArrayList&lt;&gt;();</div><div class="line">        for (int i = 0; i &lt; 100; i++) &#123;</div><div class="line">            data.add(&quot;Text &quot; + (i + 1));</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        recyclerView.setAdapter(new RecyclerView.Adapter&lt;MyViewHolder&gt;() &#123;</div><div class="line"></div><div class="line">            @Override</div><div class="line">            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) &#123;</div><div class="line">                LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);</div><div class="line">                return new MyViewHolder(layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false));</div><div class="line">            &#125;</div><div class="line"></div><div class="line">            @Override</div><div class="line">            public void onBindViewHolder(MyViewHolder holder, int position) &#123;</div><div class="line">                holder.bind(data.get(position));</div><div class="line">            &#125;</div><div class="line"></div><div class="line">            @Override</div><div class="line">            public int getItemCount() &#123;</div><div class="line">                return data.size();</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    class MyViewHolder extends RecyclerView.ViewHolder &#123;</div><div class="line"></div><div class="line">        TextView textView;</div><div class="line"></div><div class="line">        public MyViewHolder(View itemView) &#123;</div><div class="line">            super(itemView);</div><div class="line">            textView = (TextView) itemView.findViewById(android.R.id.text1);</div><div class="line">        &#125;</div><div class="line"></div><div class="line">        void bind(String text) &#123;</div><div class="line">            textView.setText(text);</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>Gif:<br><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AndroidStudioRefactor/master//100.gif" alt=""></p>
<p>After:<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div></pre></td><td class="code"><pre><div class="line">public class StartActivity extends AppCompatActivity &#123;</div><div class="line"></div><div class="line">    private RecyclerView recyclerView;</div><div class="line">    private List&lt;String&gt; data;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    protected void onCreate(Bundle savedInstanceState) &#123;</div><div class="line">        super.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_start);</div><div class="line">        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);</div><div class="line">        setSupportActionBar(toolbar);</div><div class="line"></div><div class="line">        initData();</div><div class="line">        initView();</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    private void initData() &#123;</div><div class="line">        data = new ArrayList&lt;&gt;();</div><div class="line">        for (int i = 0; i &lt; 100; i++) &#123;</div><div class="line">            data.add(&quot;Text &quot; + (i + 1));</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    private void initView() &#123;</div><div class="line">        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);</div><div class="line">        recyclerView.setLayoutManager(new LinearLayoutManager(this));</div><div class="line">        recyclerView.setAdapter(new MyViewHolderAdapter(data));</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">class MyViewHolderAdapter extends RecyclerView.Adapter&lt;MyViewHolder&gt; &#123;</div><div class="line"></div><div class="line">    private List&lt;String&gt; data;</div><div class="line"></div><div class="line">    public MyViewHolderAdapter(List&lt;String&gt; data) &#123;</div><div class="line">        this.data = data;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) &#123;</div><div class="line">        return MyViewHolder.newInstance(parent);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void onBindViewHolder(MyViewHolder holder, int position) &#123;</div><div class="line">        holder.bind(data.get(position));</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public int getItemCount() &#123;</div><div class="line">        return data.size();</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line">class MyViewHolder extends RecyclerView.ViewHolder &#123;</div><div class="line"></div><div class="line">    public static final int LAYOUT_ID = android.R.layout.simple_list_item_1;</div><div class="line"></div><div class="line">    @NonNull</div><div class="line">    static MyViewHolder newInstance(ViewGroup parent) &#123;</div><div class="line">        Context context = parent.getContext();</div><div class="line">        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);</div><div class="line">        return new MyViewHolder(layoutInflater.inflate(LAYOUT_ID, parent, false));</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    TextView textView;</div><div class="line"></div><div class="line">    public MyViewHolder(View itemView) &#123;</div><div class="line">        super(itemView);</div><div class="line">        textView = (TextView) itemView.findViewById(android.R.id.text1);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    void bind(String text) &#123;</div><div class="line">        textView.setText(text);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>至此，Android Studio的重构功能讲解完成，由于Android Studio更新很快，很多功能还需要自己去发掘。</p>
<p>『君子生非异也，善假于物也』，好好使用工具，可以偷个懒。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/代码重构/" rel="tag"># 代码重构</a>
          
            <a href="/tags/Android-Studio重构/" rel="tag"># Android Studio重构</a>
          
            <a href="/tags/Android-重构/" rel="tag"># Android 重构</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/19/Mockito/" rel="next" title="Mockito浅谈">
                <i class="fa fa-chevron-left"></i> Mockito浅谈
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/07/19/Android-Monkey/" rel="prev" title="Android Monkey整理">
                Android Monkey整理 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#使用Android-Studio对代码进行重构"><span class="nav-number">1.</span> <span class="nav-text">使用Android Studio对代码进行重构</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">2.</span> <span class="nav-text">简介</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#如何使用"><span class="nav-number">2.1.</span> <span class="nav-text">如何使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码与操作示例"><span class="nav-number">2.2.</span> <span class="nav-text">代码与操作示例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码抽取"><span class="nav-number">2.3.</span> <span class="nav-text">代码抽取</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Android资源重构"><span class="nav-number">2.4.</span> <span class="nav-text">Android资源重构</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#综合示例"><span class="nav-number">3.</span> <span class="nav-text">综合示例</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2016/07/19/Android-Studio-Refactor/';
          this.page.identifier = '2016/07/19/Android-Studio-Refactor/';
          this.page.title = '使用Android Studio对代码进行重构';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
